/**
 * 
 */
package cn.edu.hstc.bean.utils;

import java.io.File;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.net.http.AndroidHttpClient;

import cn.edu.hstc.app.Constant;
import cn.edu.hstc.bean.User;
import cn.edu.hstc.common.HttpUtils;
import cn.edu.hstc.common.L;
import cn.edu.hstc.ui.LoginJwgl;

/**
 * @Description 用户工具辅助类
 * @author act262
 * @version 1.0
 * @since 2014-4-30 上午10:00:37
 * 
 */
public class UserUtils {
	/**
	 * @param client
	 *            android http client
	 * @param info
	 *            参数1:学号 ;参数2:姓名
	 * @return
	 */
	public static User getUser(AndroidHttpClient client, String... info) {
		String response = HttpUtils.getHttpResponse(client, Constant.JWGL_USER_INFO_URL, info);
		if (null == response) {
			return null;
		} else {
			// System.out.println(response);

			return parse2User(response);
		}
	}

	/**
	 * 根据输入的内容来生成一个User对象
	 * 
	 * @param content
	 *            输入内容,从用户登陆后的页面的用户信息页面下获取内容
	 * @return 如果正常登陆的内容返回一个User,否则返回null
	 */
	private static User parse2User(String content) {
		if (null == content) {
			return null;
		}

		User user = new User();
		Document document = Jsoup.parse(content);

		user.setName(document.getElementById("xm").text());
		user.setPassword("");
		user.setStudentId(document.getElementById("xh").text());
		user.setFormerName(document.getElementById("lbl_zym").text());
		user.setGender(document.getElementById("lbl_xb").text());
		user.setBirthday(document.getElementById("lbl_csrq").text());
		user.setNation(document.getElementById("lbl_mz").text());
		user.setNativePlace(document.getElementById("lbl_jg").text());
		user.setPoliticalStatus(document.getElementById("lbl_zzmm").text());
		user.setAcademy(document.getElementById("lbl_xy").text());
		user.setDepartment(document.getElementById("lbl_xi").text());
		user.setSpecialty(document.getElementById("lbl_zymc").text());
		user.setExecutiveCourses(document.getElementById("lbl_xzb").text());
		user.setAcademicYear(document.getElementById("lbl_xz").text());
		user.setEnrollmentDate(document.getElementById("lbl_rxrq").text());
		user.setExaminationNumber(document.getElementById("lbl_zkzh").text());
		user.setID(document.getElementById("lbl_sfzh").text());
		user.setAttainment(document.getElementById("lbl_CC").text());
		user.setStudyMode(document.getElementById("lbl_xxxs").text());

		// 头像地址
		user.setHeadIcon(String.format(Constant.JWGL_HEAD_ICON_URL, LoginJwgl.randomUrl, user.getStudentId()));

		return user;
	}

	/**
	 * 获取输入文本内容的学号,用户名
	 * 
	 * @param content
	 * @return 第一个结果:学号 , 第二个结果用户名
	 */
	public static String[] getUserInfo(String content) {

		if (null != content) {
			String[] result = Jsoup.parse(content).getElementById("xhxm").text().split(" ");

			// 返回结果不能为空,且分割后为两个字符串:学号+姓名
			if (null != result && 2 == result.length) {
				result[1] = result[1].replace("同学", "").trim();
				return result;
			}
		}
		return null;
	}

	//not hstc
	public static String getUserName(String content) {
		return Jsoup.parse(content).getElementById("xhxm").text();
	}

	public static boolean updateHeadIcon(User user, File file) {

		return true;
	}

}
